import Vue from 'vue'
import Router from 'vue-router'
import Home from "@/pages/Home/Home"
import Login from "@/pages/Login/Login"
import Library from "@/pages/Library/Library"
import List from "@/pages/Library/components/List.vue"
import Root from "@/pages/Library/components/Root.vue"
import Detail from "@/pages/Detail/Detail"
import My from "@/pages/My/My"
import Project from "@/pages/Project/Project"
import Search from "@/pages/Search/Search"
import ProjectDetail from "@/pages/ProjectDetail/ProjectDetail"
import ProjectRoot from "@/pages/ProjectDetail/Root"
import ProjectList from "@/pages/ProjectDetail/List"
import Admin from "@/pages/Admin/Admin"
import Basic from "@/pages/Admin/components/Basic"
import UserAdmin from "@/pages/Admin/components/UserAdmin"
import CertificationAudit from "@/pages/Admin/components/CertificationAudit"
import Warehousing from "@/pages/Admin/components/Warehousing"
import Backup from "@/pages/Admin/components/Backup"

import { Notification } from 'element-ui';
Vue.prototype.$notification = Notification;

Vue.use(Router)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/library',
    // name: 'Library',
    component: Library,
    children:[
      {
        path:'root',
        component:Root
      },
      {
        path:'list',
        component:List
      }
    ]
  },
  {
    path:'/detail',
    name:'Detail',
    component:Detail
  },
  {
    path:'/my',
    name:'My',
    component:My
  },
  {
    path:'/project',
    name:'Project',
    component:Project
  },
  {
    path:'/project_detail',
    name:'ProjectDetail',
    component:ProjectDetail,
    children:[
      {
        path:'root',
        component:ProjectRoot
      },
      {
        path:'list',
        component:ProjectList
      }
    ]
  },
  {
    path:'/search',
    name:'Search',
    component:Search
  },
  {
    path:'/admin',
    name:'Admin',
    component:Admin,
    children:[
      {
        path:'basic',
        component:Basic
      },
      {
        path:'user_admin',
        component:UserAdmin
      },
      {
        path:'certification_audit',
        component:CertificationAudit
      },
      {
        path:'warehousing',
        component:Warehousing
      },
      {
        path:'backup',
        component:Backup
      }
    ]
  }
];
const router = new Router({
  routes
});

// 挂载全局路由导航守卫
router.beforeEach((to,from,next) => {
  // to：将要访问的路径
  // from：从哪个路径跳转而来
  // next：是一个函数，表示放行
  const login =JSON.parse(window.localStorage.getItem('login'));
  // const token = login.token;
  if( login ){ 
    //如果有就直接继续操作
    if(login.token !== ""){
      next();
    }else{
      if(to.path=='/login' || to.path == '/'){ 
        //如果是登录页面,就停留登录页
        next();
      } else { 
        //不然就跳转到登录；
        Notification({
          title: '您还未登录',
          message: '请登录！',
          type: 'warning'
        });
        next('/login');
      }
    }
  } else {
      if(to.path=='/login' || to.path == '/'){ 
        //如果是登录页面,就停留登录页
        next();
      } else { 
        //不然就跳转到登录；
        Notification({
          title: '您还未登录',
          message: '请登录！',
          type: 'warning'
        });
        next('/login');
      }
  }
  next();
});

// 管理员页面路由守卫
router.beforeEach((to, from, next) => {
  // 如果不是管理页面，直接放行，next
  if (to.path !== '/admin') {
    return next()
  }
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  // next()  放行    next('/login')  强制跳转
  const login =   JSON.parse(window.localStorage.getItem('login'));
  if (!login) {  // 不存在 转跳至登录页面
    return next('/login')
  } else {  // 存在
    if(login.type < 3){// 不是管理员用户
      Notification({
        title: '您无权访问该页面',
        message: '访问被拒绝！',
        type: 'warning'
      });
      next('/');
    }else{
      // 放行
      return next()
    }
  }
})

export default router